﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Collections;
using System.Drawing.Printing;

namespace SoundServant
{
    public class StoredDisc
    {
        public readonly int Id;
        string title;
        bool current = false;
        public readonly StoredRecording Recording;
        DirectoryInfo directory;
        TimeSpan length = new TimeSpan(0);
        ArrayList tracks = new ArrayList();
        public StoredTrack CurrentTrack = null;
        public bool Flag = false;
        bool error = false;
        int copies = 0;

        public bool Error { get { return error; } }
        public bool Current { get { return current; } }
        public ArrayList Tracks { get { return tracks; } }
        public int Number { get { return Recording.Discs.IndexOf(this) + 1; } }
        public DirectoryInfo Directory { get { return directory; } }
        public TimeSpan Length
        {
            get
            {
                if (!current)
                {
                    TimeSpan length = new TimeSpan(0);
                    foreach (StoredTrack track in tracks)
                    {
                        length += track.Length;
                    }
                    return length;
                }
                else
                {
                    return Recording.Recorder.RecorderDiscTimePassed;
                }
            }
        }

        public int Copies
        {
            get
            {
                return copies;
            }
        }

        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                SqlConnection connection = SS.Connection();
                SqlCommand dbCommand = new SqlCommand("UPDATE discs SET discTitle = '" + SS.MakeSQLSafe(title) + "' WHERE discId = " + Id, connection);
                dbCommand.ExecuteNonQuery();
                if (Updated != null) Updated();
                connection.Close();
                connection.Dispose();
            }
        }
        public string Summary
        {
            get
            {
                string summary = "Disc " + Number + "(" + SS.ParseTimeSpan(length) + ")";
                foreach (StoredTrack track in tracks)
                {
                    if (!track.IsSong)
                    {
                        summary += ", " + track.Title;
                    }
                }
                return summary;
            }
        }

        public delegate void VoidEventHandler();
        public event VoidEventHandler Updated;
        public event VoidEventHandler Finished;
        public event VoidEventHandler Deleted;
        public delegate void NewTrackCreatedEventHandler();
        public event NewTrackCreatedEventHandler NewTrackCreated;

        public StoredDisc(StoredRecording r, int i)
        {
            Id = i;
            Recording = r;
            directory = new DirectoryInfo(Recording.Directory.FullName + "\\" + Id);

            if (directory.Exists)
            {
                SqlCommand dbCommand;
                SqlDataReader dbReader;
                SqlConnection connection = SS.Connection();
                dbCommand = new SqlCommand("SELECT discTitle, discCopies FROM discs WHERE discId = " + Id, connection);
                dbReader = dbCommand.ExecuteReader();
                dbReader.Read();

                title = dbReader["discTitle"].ToString();
                copies = (int)dbReader["discCopies"];

                dbReader.Close();

                dbCommand = new SqlCommand("SELECT trackID FROM tracks WHERE trackDisc = " + Id, connection);
                dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows) while (dbReader.Read())
                    {
                        StoredTrack track = new StoredTrack((int)dbReader[0], this);
                        tracks.Add(track);
                        if (track.Error) error = true;

                    }
                dbReader.Close();
                connection.Close();
                connection.Dispose();
            }
        }
        public void IncrementCopies()
        {
            copies++;
            SqlConnection connection = SS.Connection();
            SqlCommand dbCommand = new SqlCommand("UPDATE discs SET discCopies = " + copies + " WHERE discId = " + Id, connection);
            dbCommand.ExecuteNonQuery();
            if (Updated != null) Updated();
            connection.Close();
            connection.Dispose();
        }

        public StoredDisc(StoredRecording r, string t, out int i)
        {
            title = t;
            Recording = r;

            current = true;
            SqlConnection connection = SS.Connection();
            SqlCommand dbCommand = new SqlCommand("INSERT INTO discs (discTitle, discRec) VALUES ('" + title + "', '" + Recording.Id + "') SELECT CAST(scope_identity() AS int)", connection);
            Id = (int)dbCommand.ExecuteScalar();
            connection.Close();
            connection.Dispose();
            i = Id;

            directory = new DirectoryInfo(Recording.Directory.FullName + "\\" + Id);
            directory.Create();
            if (Recording.CurrentDisc != null) Recording.CurrentDisc.Finish();
            Recording.CurrentDisc = this;
            NewTrackCreated += new NewTrackCreatedEventHandler(Recording.NewTrackCreatedProxy);
        }

        public string CreateNewTrack(string t)
        {
            string newTrackPath;
            tracks.Add(new StoredTrack(this, t, out newTrackPath));
            if (NewTrackCreated != null) NewTrackCreated();
            return newTrackPath;
        }

        public void Finish()
        {
            if (current)
            {
                CurrentTrack.Finish();
                current = false;
                if (Finished != null) Finished();
            }
        }

        public void Delete()
        {
            if (!current)
            {
                foreach (StoredTrack track in tracks)
                {
                    track.Delete();
                }

                directory.Delete(true);
                SqlConnection connection = SS.Connection();
                SqlCommand dbCommand = new SqlCommand("DELETE FROM discs WHERE discId = " + Id, connection);
                dbCommand.ExecuteNonQuery();
                connection.Close();
                connection.Dispose();
                if (Deleted != null) Deleted();
            }
        }

        public void PrintLabel()
        {
            PrintDocument label = new PrintDocument();
            label.PrintPage += new PrintPageEventHandler(Label_Print);
            label.Print();
        }

        void Label_Print(object sender, PrintPageEventArgs e)
        {

        }
    }
}
